var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        permissions:['courseClass:add','courseClass:update'],//本页权限数组
        content:'',
        desc:'',
        selects:[],
        schoolId:schoolId,
        schoolList:schoolMap,

        classVo:{},
        courseCopy:{},
        // courseCopySpec:{},
        courseAndClass:{},
        courseAndClassList:[{teacherId:'',teacherType:''}],
        specList:[{id:'',lessonNum:'',price:'',unitName:''}],

        classRoomList:[],
        courseList:[],
        teacherList:[],
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200);

            return ;
        }

        that.getClassRoomList();
        that.getCourseList();
        that.getTeacherList();
        that.getDicList();
        that.getTagList();

        if(!empty(aGET.id)){
            setTimeout(function () {
                that.getInfoById(aGET.id);
            },800)
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        // 得到教室
        getClassRoomList: function() {
            var that = this;
            Get(that, _HOST + _API.organization_classroom_getClassRoomBy + "?companyId="+companyId+"&schoolId="+schoolId)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        that.classRoomList = response.list;
                    }
                });
        },
        // 得到课程
        getCourseList: function() {
            var that = this;
            Get(that, _HOST + _API.organization_course_getCourseBySchoolId + "?schoolId="+schoolId)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        that.courseList = response.list;
                    }
                });
        },
        // 得到教师列表
        getTeacherList: function() {
            var that = this;
            Get(that, _HOST + _API.organization_teacher_getTeacherBy + "?companyId="+companyId+"&schoolId="+schoolId)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        that.teacherList = response.list;
                    }
                });
        },
        // 得到字典map
        getDicList: function() {
            var that = this;
            Get(that,_HOST+_API.common_dic_dicList+"?keyTable=courseType")
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            that.selects = response.list;
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                });
        },
        getTagList: function () {
            var that = this;
            Get(that, _HOST + _API.common_tag_getTagsByType + "?type=2&companyId=" + companyId + "&schoolId=" + schoolId)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        initSelect(response.list);
                    }
                });
        },
        getInfoById:function (id) {
            var that = this;
            Get(that,_HOST+_API.organization_courseClass_getById+"?id="+id)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            var data = response.data;
                            // 填入数据
                            that.classVo = data;
                            that.courseCopy = data.courseCopy;
                            if (data.courseAndClassList != null && data.courseAndClassList.length > 0) {
                                that.courseAndClass = data.courseAndClassList[0];
                                that.courseAndClassList = data.courseAndClassList;
                            }
                            if (data.specList != null && data.specList.length > 0) {
                                that.specList = data.specList;
                            }
                            if (data.tags != null && data.tags.length > 0) {
                                selectTags(data.tags);
                            }
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        // 添加
        add:function () {
            var that = this;
            var param = that.classVo;
            param.schoolId = that.schoolId;
            param.companyId = companyId;

            param.courseCopy = that.courseCopy;
            param.courseCopy.schoolId = that.schoolId;
            param.courseCopy.companyId = companyId;

            param.courseAndClass = that.courseAndClass;
            param.courseAndClass.schoolId = that.schoolId;
            param.courseAndClass.companyId = companyId;

            var classList = that.courseAndClassList;
            for (let i = 0; i < classList.length; i++) {
                classList[i].schoolId = that.schoolId;
                classList[i].companyId = companyId;
                classList[i].courseId = that.courseAndClass.courseId;
            }
            if (classList == null || classList.length == 0) {
                var obj = {};
                obj.schoolId = that.schoolId;
                obj.companyId = companyId;
                obj.courseId = that.courseAndClass.courseId;
                classList.push(obj);
            }
            param.courseAndClassList = classList;

            param.specList = that.specList;
            // 验证表单
            if (empty(param.name)) {
                layer.msg("请输入班级名称", {time: 1500, icon:5});
                return;
            } else if (empty(param.beginTime)) {
                layer.msg("请选择开班时间", {time: 1500, icon:5});
                return;
            } else if (empty(param.endTime)) {
                layer.msg("请选择结班时间", {time: 1500, icon:5});
                return;
            } else if (empty(param.classRoom)) {
                layer.msg("请选择上课教室", {time: 1500, icon:5});
                return;
            } else if (empty(param.nowCourse)) {
                layer.msg("请输入当前课时", {time: 1500, icon:5});
                return;
            } else if (empty(param.allCourse)) {
                layer.msg("请输入总课时", {time: 1500, icon:5});
                return;
            } else if (empty(param.addr)) {
                layer.msg("请选择上课教室详细地址", {time: 1500, icon:5});
                return;
            } else if (empty(that.courseAndClass.courseId)) {
                layer.msg("请选择课程", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.beginApply)) {
                layer.msg("请选择开始报名时间", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.endApply)) {
                layer.msg("请选择结束报名时间", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.isAudition)) {
                layer.msg("请选择是否开启试听", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.isSynchro)) {
                layer.msg("请选择是否同步现有课程班进行试听", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.studentAuditionNum)) {
                layer.msg("请选择小孩可试听次数", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.stageNum)) {
                layer.msg("请输入当前期数", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.stageSumNum)) {
                layer.msg("请输入总期数", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.status)) {
                layer.msg("请选择课程状态", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.type)) {
                layer.msg("请选择课程类别", {time: 1500, icon:5});
                return;
            }
            // 得到标签
            var tagArr = $("#tags").val();
            var ids = "";
            if (!empty(tagArr)) {
                for (let i = 0; i < tagArr.length; i++) {
                    ids += tagArr[i];
                    if ((i + 1) != tagArr.length) {
                        ids += ",";
                    }
                }
            }
            param['tags'] = ids;
             //console.log(param);
            Post(that,_HOST+_API.organization_courseClass_add,param,true,that.permissions[0])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        // 修改
        update:function () {
            var that = this;
            var param = that.classVo;
            param.schoolId = that.schoolId;
            param.companyId = companyId;

            param.courseCopy = that.courseCopy;
            param.courseCopy.schoolId = that.schoolId;
            param.courseCopy.companyId = companyId;

            var classList = that.courseAndClassList;
            for (let i = 0; i < classList.length; i++) {
                classList[i].schoolId = that.schoolId;
                classList[i].companyId = companyId;
                classList[i].courseId = that.courseAndClass.courseId;
            }
            if (classList == null || classList.length == 0) {
                var obj = {};
                obj.schoolId = that.schoolId;
                obj.companyId = companyId;
                obj.courseId = that.courseAndClass.courseId;
                classList.push(obj);
            }
            param.courseAndClassList = classList;

            param.specList = that.specList;
           //console.log(param);
            // 验证表单
            if (empty(param.name)) {
                layer.msg("请输入班级名称", {time: 1500, icon:5});
                return;
            } else if (empty(param.beginTime)) {
                layer.msg("请选择开班时间", {time: 1500, icon:5});
                return;
            } else if (empty(param.endTime)) {
                layer.msg("请选择结班时间", {time: 1500, icon:5});
                return;
            } else if (empty(param.classRoom)) {
                layer.msg("请选择上课教室", {time: 1500, icon:5});
                return;
            } else if (empty(param.nowCourse)) {
                layer.msg("请输入当前课时", {time: 1500, icon:5});
                return;
            } else if (empty(param.allCourse)) {
                layer.msg("请输入总课时", {time: 1500, icon:5});
                return;
            } else if (empty(param.addr)) {
                layer.msg("请选择上课教室详细地址", {time: 1500, icon:5});
                return;
            } else if (empty(that.courseAndClass.courseId)) {
                layer.msg("请选择课程", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.beginApply)) {
                layer.msg("请选择开始报名时间", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.endApply)) {
                layer.msg("请选择结束报名时间", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.isAudition)) {
                layer.msg("请选择是否开启试听", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.isSynchro)) {
                layer.msg("请选择是否同步现有课程班进行试听", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.studentAuditionNum)) {
                layer.msg("请选择小孩可试听次数", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.stageNum)) {
                layer.msg("请输入当前期数", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.stageSumNum)) {
                layer.msg("请输入总期数", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.status)) {
                layer.msg("请选择课程状态", {time: 1500, icon:5});
                return;
            } else if (empty(param.courseCopy.type)) {
                layer.msg("请选择课程类别", {time: 1500, icon:5});
                return;
            }
            // 得到标签
            var tagArr = $("#tags").val();
            var ids = "";
            if (!empty(tagArr)) {
                for (let i = 0; i < tagArr.length; i++) {
                    ids += tagArr[i];
                    if ((i + 1) != tagArr.length) {
                        ids += ",";
                    }
                }
            }
            param['tags'] = ids;
             //console.log(param)
            Post(that,_HOST+_API.organization_courseClass_update,param,true,that.permissions[1])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        // 选择课程
        selectCourse: function() {
            var that = this;
            var id = that.courseAndClass.courseId;
            Get(that, _HOST + _API.organization_course_getById + "?id="+id)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        var course = response.data;
                        // 填入数据
                        var courseCopy = that.courseCopy;
                        courseCopy.imgId = course.imgId;
                        courseCopy.star = course.star;
                        courseCopy.shortDesc = course.shortDesc;
                        courseCopy.description = course.description;
                        courseCopy.name = course.name;
                        courseCopy.suit = course.suit;
                        // that.courseCopy = courseCopy;
                        that.$set(that.courseCopy,courseCopy);
                        console.log(that.courseCopy);
                    }
                });
        },
        // 规格相关
        addSpec: function () {
            var that = this;
            that.specList.push({id:'',lessonNum:'',price:'',unitName:''});
        },
        delSpec: function(index) {
            var that = this;
            that.specList.splice(index,1);
        },
        getSpecList: function () {
            var that = this;
            console.log(that.specList);
        },
        // 老师相关
        addTeacher: function () {
            var that = this;
            that.courseAndClassList.push({teacherId:'',teacherType:''});
        },
        delTeacher: function(index) {
            var that = this;
            that.courseAndClassList.splice(index,1);
        },
        getTeachers: function () {
            var that = this;
            console.log(that.courseAndClassList);
        },
        minValue(index, type) {
            var that = this;
            if (1 == type) {
                if (!ckPrice(that.specList[index].price)) {
                    that.specList[index].price = '';
                }
            } else if (3 == type) {
                if (!ckPrice(that.specList[index].oldPrice)) {
                    that.specList[index].oldPrice = '';
                }
            } else if (4 == type) {
                if (!ckPrice(that.specList[index].prePrice)) {
                    that.specList[index].prePrice = '';
                }
            }
        },
    },
});

// 设置时间
function setTimes() {
    //var that = this;
    var name = $(this).attr("name");
    if (name == 'classVo.beginTime') {
        app.classVo.beginTime = $(this).val();
    } else if (name == 'classVo.endTime') {
        app.classVo.endTime = $(this).val();
    } else if (name == 'courseCopy.endApply') {
        app.courseCopy.endApply = $(this).val();
    } else if (name == 'courseCopy.beginApply') {
        app.courseCopy.beginApply = $(this).val();
    }
   /* $(".time").each(function(){

    });*/
}

// 初始化多选
function initSelect(datas) {
    var select = $("#tags");
    for (var i = 0; i < datas.length; i++) {
        select.append("<option value='" + datas[i].id + "'>"
            + datas[i].name + "</option>");
    }
    select.selectpicker('val', '');
    select.selectpicker('refresh');
}
// 选中tag
function selectTags(value) {
    var s = document.getElementById("tags");
    console.log(value);
    if (!empty(value)) {
        var tagsArr = value.split(",");
        $('.selectpicker').selectpicker('val', tagsArr);
    }
}

// $('#Form').data('bootstrapValidator', null);
Validator();
function Validator() {
    $("#Form").submit(function(e){
        e.preventDefault();
        if(empty(aGET.id)){
            app.add();
        }else {
            app.update();
        }
    });
}
